﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _14块操作
{
    public class Class1
    {
        //简单块
        [CommandMethod("BlockDemo")]
        public void BlockDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            //using (Transaction trans = db.TransactionManager.StartTransaction())
            //{
            //    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
            //    foreach (var item in bt)
            //    {
            //        BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);
            //    }
            //}
            MyBlockTableRecord.Block1Id = db.AddBlockTableRecord(MyBlockTableRecord.Block1Name, MyBlockTableRecord.Block1Ents);
            
        }

        [CommandMethod("InsertBlockDemo")]
        public void InsertBlockDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(10, 10, 0));
            db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(40, 10, 0),Math.PI/4,new Scale3d(2));
            db.InsertBlockBlockReference(MyBlockTableRecord.Block1Id, new Point3d(100, 10, 0), Math.PI / 4, new Scale3d(2,1.5,1));
        }

        //注释样式的箭头
        [CommandMethod("ModifyDimDemo")]
        public void ModifyDimDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            MyBlockTableRecord.DimBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.DimBlock1Name, MyBlockTableRecord.DimBlock1Ents);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                
                DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                if (dst.Has("Standard"))
                {
                    DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);
                    if (MyBlockTableRecord.DimBlock1Id != ObjectId.Null)
                    {
                        dstr.UpgradeOpen();
                        dstr.Dimblk = MyBlockTableRecord.DimBlock1Id;
                        dstr.Dimasz = 1;
                        dstr.DowngradeOpen();
                        db.SetDimstyleData(dstr);
                    }
                }
                trans.Commit();
            }
        }

        //属性快
        [CommandMethod("AttrBlockDemo")]
        public void AttrBlockDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);
            ObjectId brId = db.InsertAttrBlockReference(MyBlockTableRecord.AttrBlock1Id, new Point3d(10, 10, 0), 0, new Scale3d(1, 1, 1));

            Dictionary<string, string> attrNameValues = new Dictionary<string, string>();
            attrNameValues.Add("编号", "天线11");
            attrNameValues.Add("功率", "10.0dBm");
            attrNameValues.Add("功率222", "10.0dBm");
            brId.UpdateBlockAttr(attrNameValues);

        }


        [CommandMethod("TestDemo")]
        public void TestDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            MyBlockTableRecord.AttrBlock1Id = db.AddBlockTableRecord(MyBlockTableRecord.AttrBlock1Name, MyBlockTableRecord.AttrBlock1Ents);
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                //DimStyleTable dst = (DimStyleTable)trans.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                //if (dst.Has("Standard"))
                //{
                //    DimStyleTableRecord dstr = (DimStyleTableRecord)dst["Standard"].GetObject(OpenMode.ForRead);
                //}
                foreach (var item in bt)
                {
                    if (bt.Has(MyBlockTableRecord.AttrBlock1Id))
                    {
                        if (item  == MyBlockTableRecord.AttrBlock1Id)
                        {
                            BlockTableRecord btr = (BlockTableRecord)item.GetObject(OpenMode.ForRead);

                            foreach (var item1  in btr)
                            {
                                if (item1.GetObject(OpenMode.ForRead) is AttributeDefinition)
                                {
                                    AttributeDefinition attr = (AttributeDefinition)item1.GetObject(OpenMode.ForRead);
                                }
                                
                            }
                        }  
                    }
                    
                }
            }
        }

        [CommandMethod("PickDemo")]
        public void PickDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptEntityResult per = ed.GetEntity("选择块");
            if (per.Status == PromptStatus.OK)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockReference br = (BlockReference)per.ObjectId.GetObject(OpenMode.ForRead);
                } 
            }
        }
       
    }
}
